import 'package:flutter/material.dart';
import 'package:charts_flutter/flutter.dart' as charts;
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:material_collection_app/utils/utils.dart';

class RingChart extends StatefulWidget {
  final List data;

  RingChart({Key key, this.data}) : super(key: key);

  @override
  State<StatefulWidget> createState() => MyState();
}

// class TimeSeriesSales {
//   final DateTime time;
//   final int sales;

//   TimeSeriesSales(this.time, this.sales);
// }

class MyState extends State<RingChart> {

  List<LinearSales> serialdata = [
    // new LinearSales('新增', 1, 30, Utils.hexToColor('#1BC85E')),
    // new LinearSales('办结', 2, 45, Utils.hexToColor('#7877FF')),
    // new LinearSales('延期', 3, 25, Utils.hexToColor('#FEA24B')),
    // new LinearSales('待办', 4, 20, Utils.hexToColor('#20B0F8')),
  ];

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false);

    List list = widget.data;
    list.sort();
    var maxNum = list[list.length-1];

    if(maxNum > 0) {
      serialdata = [
        new LinearSales('新增', 1, widget.data[0], Utils.hexToColor('#1BC85E')),
        new LinearSales('办结', 2, widget.data[1], Utils.hexToColor('#7877FF')),
        new LinearSales('延期', 3, widget.data[2], Utils.hexToColor('#FEA24B')),
        new LinearSales('待办', 4, widget.data[3], Utils.hexToColor('#20B0F8')),
      ];
    }else {
      serialdata = [
        new LinearSales('新增', 1, 1, Utils.hexToColor('#1BC85E')),
        new LinearSales('办结', 2, 1, Utils.hexToColor('#7877FF')),
        new LinearSales('延期', 3, 1, Utils.hexToColor('#FEA24B')),
        new LinearSales('待办', 4, 1, Utils.hexToColor('#20B0F8')),
      ];
    }

    List<charts.Series<LinearSales, int>> seriesList =  [
      new charts.Series<LinearSales, int>(
        id: 'Sales',
        domainFn: (LinearSales sales, _) => sales.year,
        measureFn: (LinearSales sales, _) => sales.sales,
        data: serialdata,
        colorFn: (LinearSales sales, _) =>   charts.ColorUtil.fromDartColor(sales.color),
        outsideLabelStyleAccessorFn: (LinearSales sales, _) =>
         charts.TextStyleSpec(
            fontSize: 12,
            color: charts.ColorUtil.fromDartColor(sales.color),
        ),
        insideLabelStyleAccessorFn: (LinearSales sales, _) =>
         charts.TextStyleSpec(
            color: charts.ColorUtil.fromDartColor(Colors.orange),
        ),
        labelAccessorFn: (LinearSales row, _) => '今日${row.title}\n${maxNum > 0 ? row.sales : 0}',
      )
    ];

    var chart = charts.PieChart(
      seriesList,
      animate: true,
      defaultRenderer: new charts.ArcRendererConfig(
        arcWidth: 30,
        arcRendererDecorators: [new charts.ArcLabelDecorator()]
      )
    );

    return  Container(
      child: Stack(
        children: <Widget>[
          chart,
          Container(
            child: Align(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Container(
                    margin: EdgeInsets.only(top: 20.w),
                    child: Text(widget.data[3].toString(), style: TextStyle(
                      color: Utils.hexToColor('#20AFF7'),
                      fontSize: 50.w,
                      fontWeight: FontWeight.bold
                    )),
                  ),
                  Text('待办任务', style: TextStyle(
                    color: Utils.hexToColor('#CBCBCB'),
                    fontSize: 12,
                  ))
                ],
              ),
            ),
          )
        ],
      )
    );
  }
}

class LinearSales {
  final String title;
  final int year;
  final int sales;
  final Color color;

  LinearSales(this.title, this.year, this.sales, this.color);
}